package pers.xuyong.strive.shiro.service;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.xuyong.strive.shiro.util.ShiroUtil;

/**
 * 用户服务
 *
 * @author billymac
 * @version 1.0
 * @date 17/3/19  下午8:55
 * @since JDK1.8
 */
public class UserService {

    private Logger logger = LoggerFactory.getLogger(UserService.class);

    ShiroUtil shiroUtil;

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    public String login(String username, String password) {
        if (shiroUtil == null)
            shiroUtil = ShiroUtil.getInstance();

        Subject subject = shiroUtil.getSubject();

        try {
            subject.login(shiroUtil.getToken(username, password));
        } catch (AuthenticationException var1) {
            logger.warn("login failed!", var1);
            return "login failed!";
        }
        return "login success!";
    }

    /**
     * 用户退出登陆
     * @return
     */
    public String logout() {
        if (shiroUtil == null)
            shiroUtil = ShiroUtil.getInstance();

        Subject subject = shiroUtil.getSubject();

        try {
            subject.logout();
        } catch (AuthenticationException var1) {
            logger.warn("logout failed!", var1);
            return "logout failed!";
        }
        return "logout success!";
    }
}
